<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>变更职位</title>
		<link rel="stylesheet" type="text/css" href="../static/common.css" />
		<link rel="stylesheet" type="text/css" href="static/css/common.css" />
		<link rel="stylesheet" type="text/css" href="static/css/style.css" />
	</head>
	<body>
		
		<div class="newwork" id="newWork" v-cloak>
			<!-- <ul class="clearfix worknav">
				<li class="fl" @click="toUrl">添加身份</li>
				<li class="fl active">变更身份</li>
			</ul>
			<h3>变更职位</h3> -->
			<a href="identityinformationlist.html" class="back">我的身份信息列表</a>
			<ul class="clearfix">
				<p class="fl clearfix">
					<em>*</em> 选择单位：
				</p>
				<select name="" class="fl" v-model="state">
					<option value="1" v-if="!unit">公司</option>
					<option value="0" v-if="!pro">项目部</option>
				</select>
			</ul>

			<ul class="clearfix" v-if="isWork">
				<p class="fl clearfix">
					<em>*</em> 选择部门：
				</p>
				<select name="" class="fl" v-model="projet">
					<option value=''>请选择</option>
					<option v-for='val,index in projects' :value="val.projectId">{{val.projectName}}</option>
				</select>
			</ul>
			<ul class="clearfix" v-if="isWork">
				<p class="fl clearfix">
					<em>*</em> 选择职位：
				</p>
				<select name="" class="fl" v-model="worker">
					<option value="">请选择</option>
					<option v-for="val,index in workers" :value="val.jid">{{val.jobName}}</option>
				</select>
			</ul>
			<ul class="clearfix" v-if="isWork" v-show="isSurper">
				<p class="fl clearfix">
					<em>*</em> 是否主管：
				</p>
				<label class="fl" style="margin-left: 10px;"><input type="radio" value="1" name="1" @click="radioClick" /> 是</label>
				<label class="fl" style="margin-left: 30px;"><input type="radio" value="0" name="1" checked @click="radioClick" /> 否</label>

			</ul>
			<p class="errl">{{errmesage}}</p>
			<button id="" class="submitb pointer" @click="addSub">提交申请</button>
		</div>
	</body>
</html>
<script src="../static/js/jquery-1.8.3.js" type="text/javascript" charset="utf-8"></script>
<script src="../static/dx.js" type="text/javascript" charset="utf-8"></script>
<script src="../static/js/vue.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
	var vm = new Vue({
		el: '#newWork',
		created: function() {
			this.isShow();
			this.isUnit();
			if (this.state == 1) {
				this.getProjects();
			}


		},
		data: {
			'isSurper':true,// 是否主管 显示与否 部领导不显示
			'pro': false, //判断是否有项目部角色
			'unit': false, //判断是否有机关角色
			'errmesage': '',
			'isshow': false,
			'state': 1, //公司是1 项目部0
			'isWork': true, 
			'isSupervisor': 0,//是否主管 0不是 1是
			'projet': '', //部门id
			'worker': '', //职位id
			'projectid': 1, //项目部的默认 公司0

			'projects': [], //部门数组
			'workers': [], //职位数组
			// 'projectlist': [], //项目部数组
		},
		methods: {
			isShow: function() { //控制隐藏 是否为公司
				this.isshow = this.state == 1 ? false : true;
			},
			radioClick: function(event) { //单选 选择是否主管

				this.isSupervisor = event.target.value;
			},
			isUnit: function() { //初始化加载 判断可以变更什么职位
				var $this = this;
				DX.ajax_method({
					'type': "GET",
					'param': {},
					'url': '/user/emp/hasUnitAndProject',
					'callBack': function(res) {
						if (res.code == '200') {
						
							console.log(res);
							// 有机关且有项目部
							if (res.data.unit == true && res.data.project == true) {
								$this.state = 1;
								$this.unit = false;
								$this.pro = false;
								
							}
							// 没机关且有项目部
							if (res.data.unit == false && res.data.project == true) {
								$this.state = 0;
								$this.unit = true;
								$this.pro = false;
								
							}
							
							// 有机关且没项目部
							if (res.data.unit == true && res.data.project == false) {
								$this.pro = true;
								$this.state = 1;
								$this.unit = false;
								
							}

							
						}
					}
				})
			},
			getProjects: function() { //获取部门（切换） 
				var $this = this;
				var url = '/user/project/anon/getSubProjects';
				if ($this.state == 1) {
					url = '/user/unit/anon/findUnitDepart'
				}

				DX.ajax_method({
					'type': "GET",
					'param': {},
					'url': url,
					'callBack': function(res) {
						if (res.code == '200') {
							console.log(res);
							$this.projects = res.data;
						}
					}
				})
			},
			getWorker: function() { //获取职位
				var $this = this;
				var prid = $this.projectid;
				if ($this.state == 1) {
					var prid = 0;
				}
				DX.ajax_method({
					'type': "GET",
					'param': {
						'proid': prid,
						'departId': $this.projet
					},
					'url': '/user/job/anon/findJobsForRegister',
					'callBack': function(res) {
						if (res.code == '200') {
							console.log(res);
							$this.workers = res.data;
						}
					}
				})
			},
			addSub: function(values) { //提交数据
				this.errmesage = '';
				var $this = this;
				if (this.projet == '') {
					this.errmesage = '请选择部门';
					return;
				}
				if (this.worker == '') {
					this.errmesage = '请选择职位';
					return;
				}
				var obj = {
					 'isSupervisor':$this.isSupervisor,
					 'proid':$this.projectid,
					 'departId':$this.projet,
					 'jid':$this.worker
				};
			
				if($this.state == 1){ //机关0 项目部1
					obj.proid = 0;
				}

				
				console.log(obj);
				DX.ajax_method({
					'type': 'POST',
					'param': obj,
					'change':values.target,
					'url': '/user/emp/changeJob',
					'callBack': function(res) {
						if (res.code == '200') {
							console.log(res);
							$this.errmesage = "已提交成功，请等待审核";
						
						} else {
							$this.errmesage = res.msg;
						}
					}
				})
			},
			toUrl:function(){
				window.location.href='newwork.html';
			}
		},
		watch: {
			state:function(newval, oldval) {//项目部 机关切换
				console.log(newval);
				this.projet = '';
				this.worker = '';
				this.getProjects();
				this.workers = [];
			},
			projet:function(newval, oldval) { //部门id
				if (newval != '') {
					this.getWorker();
				}else{
					this.worker = '';
				}
				if(newval == 1){
					this.isSurper = false;
				}else{
					this.isSurper = true;
				}
			}
		}
	})
</script>
